<?php

/*
 * B端操作功能模块
 * 功能：登陆注册
 */

namespace Home\Controller;

use Think\Controller;

class PassportController extends Controller {

    public function __construct() {
        parent::__construct();
        $this->p = D('Passport', 'Logic');
    }

    public function index() {
        if ($this->p->isLogin()) {
            redirect('/');
        }
        $this->display('login');
    }
    
    public function login(){
        if ($this->p->isLogin()) {
            redirect('/');
        }
        layout('passport_layout');
        $this->display('login');
    }

    public function sublogin() {
        if ($this->p->isLogin()) {
            redirect('/');
        }
        $account = $_POST['account'];
        $password = $_POST['password'];
        if($this->p->login($account,$password)){
            redirect('/');
        }else{
            redirect('/home/passport/login?msg=1');
        }
    }
    
    //废弃注册改用申请
    public function register(){
        //if ($this->p->isLogin()) {
            redirect('/');
        //}
        $this->display('register');
    }
    
    //废弃注册改用申请
    public function subregister(){
        //if ($this->p->isLogin()) {
            redirect('/');
        //}
        $account = $_POST['account'];
        $password = $_POST['password'];
        $repassword = $_POST['repassword'];
        $user_name = $_POST['user_name'];
        
        if(!is_mobile($account)){
            redirect("/");
        }
        if(!$this->p->checkAccount($account)){
            $this->_msg('账号已存在');
        }
        $length = strlen($password);
        if($length<6 || $length>12){
            $this->_msg('密码长度6-12位');
        }
        if($password != $repassword){
            $this->_msg('密码不一致');
        }
        
        if(!$this->p->addUser($user_name,$account,$password)){
            $this->_msg('_SYSTEM_ERROR_');
        }
        if($this->p->login($account,$password)){
            redirect('/');
        }else{
            $this->_msg('_SYSTEM_ERROR_');
        }
    }
    
    public function logout(){
        session_destroy();
        redirect("/");
    }
    
    public function getuser(){
        echo $this->p->getUser();
        exit;
    }
    
    public function checkaccount(){
        $account = $_GET['account'];
        echo $this->p->checkAccount($account);
    }
    
    public function repassword(){
        if (!$this->p->isLogin()) {
            redirect('/');
        }
        $_SESSION['s_verify'] = time();
        $user = $this->p->getUserById($_SESSION['id']);
        $this->assign('mobile',substr($user['account'],0,3).'****'.substr($user['account'],-4));
        $this->display('repassword');
    }
    
    public function subrepassword(){
        if (!$this->p->isLogin()) {
            redirect('/');
        }
        $passpord = trim($_POST['password']);
        $repasspord = trim($_POST['repassword']);
        $code = (int)$_POST['code'];
        $user = $this->p->getUserById($_SESSION['id']);
        if($passpord != $repasspord){
            redirect('/home/passport/repassword?msg=1');
        }
        import('MsgCode','Msgcode');
        $m = new \Msgcode\MsgCode(true);
        if(!$m->checkCode($user['account'], $code)){
            redirect('/home/passport/repassword?msg=2');
        }
        $re = $this->p->updateUser(array('id'=>$_SESSION['id'],'password'=>$passpord));
        if($re===false){
            $this->assign('msg',L('_PASSWORD_REPLACE_FAIL_'));
        }else{
            $this->assign('msg',L('_PASSWORD_REPLACE_SUCCESS_'));
        }
        $this->display('msg');
    }
    
    public function forgetpassword(){
        $_SESSION['s_verify'] = time();
        $this->display('forgetpassword');
    }
    
    public function subforgetpassword(){
        $passpord = trim($_POST['password']);
        $repasspord = trim($_POST['repassword']);
        $code = (int)$_POST['code'];
        $mobile = trim($_POST['mobile']);
        if($passpord != $repasspord){
            redirect('/home/passport/forgetpassword?msg=1');
        }
        import('MsgCode','Msgcode');
        $m = new \Msgcode\MsgCode(true);
        if(!$m->checkCode($mobile, $code)){
            redirect('/home/passport/forgetpassword?msg=2');
        }
        $re = $this->p->updateUserByAccount(array('account'=>$mobile,'password'=>$passpord));
        if($re===false){
            $this->assign('msg',L('_PASSWORD_REPLACE_FAIL_'));
        }else{
            $this->assign('msg',L('_PASSWORD_REPLACE_SUCCESS_'));
        }
        $this->display('msg');
    }
    
    /*发短信*/
    public function code(){
        if($_GET['op'] == 1){
            $user = $this->p->getUserById($_SESSION['id']);
            $mobile = $user['account'];
        }else{
            $mobile = trim($_GET['mobile']);
            $user = $this->p->getUserByAccount($mobile);
            if($user['id']<1){
                exit('无此账号');
            }
        }
        if(!is_mobile($mobile)){
            exit('手机号错误');
        }
        import('MsgCode','Msgcode');
        $m = new \Msgcode\MsgCode(true);
        $re = $m->sendMsg_b($mobile);
        if($re){
            echo "true";
            exit;
        }
        exit('发送失败');
    }
    
    public function checkCode(){
        $code = (int)$_GET['code'];
        if($_GET['op'] == 1){
            $user = $this->p->getUserById($_SESSION['id']);
            $mobile = $user['account'];
        }else{
            $mobile = trim($_GET['mobile']);
        }
        import('MsgCode','Msgcode');
        $m = new \Msgcode\MsgCode(true);
        if(!$m->checkCode($mobile, $code)){
            exit('false');
        }
        exit('true');
    }
    protected function _msg($msg){
        $this->assign('msg',L($msg));
        $this->display('msg');
        exit;
    }

    /*商家申请入驻*/
    public function apply(){
        layout('passport_layout');
        $this->display('apply');
    }

    public function apply_submit(){
        $data['mobile'] = trim($_GET['mobile']);
        $data['name'] = $_GET['name'];
        $data['store_name'] = $_GET['store_name'];
        if(strlen($data['name'])<1 || strlen($data['store_name'])<1 || !is_mobile($data['mobile'])){
            echo false; exit;
        }
        $b = new \Home\Logic\Business_addLogic();
        $re = $b->add($data);
        if($re){
            echo true;
        }else{
            echo false;
        }
        exit;
    }

}
